﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Test.Service;
using Test.Service.Abstracts;
using Test.Service.Abstracts.ApplicationFeature;
using Test.Service.Abstracts.ApplicationFeature.Dtos;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Application.Services;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Users;
using TianShu.WebApi;

namespace Test.WebApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class HomeController : TianShuCrudController<Guid,ApplicationListOutput,ApplicationCreateUpdateInput>
    {
        public IEamApplicationService AppService { get; set; }

        public HomeController(IEamApplicationService appService,ICrudAppService<ApplicationListOutput,Guid,ApplicationCreateUpdateInput> crudAppService) : base(crudAppService)
        {
            this.AppService = appService;
        }

        [HttpGet]
        public async Task<PageResultDto<ApplicationListOutput>> Get()
        {
            var urlparam = this.Request.Query.GetUrlParams();
            var resultDto = await AppService.GetUrlParamAsync(urlparam);
            return resultDto;
        }

        [HttpPut]
        public async Task<ResultDto<ApplicationListOutput>> Insert([FromBody] ApplicationCreateUpdateInput input)
        {
            var result = await AppService.CreateAsync(input);
            return result;
        }

        [HttpDelete]
        public async Task<ResultDto<bool>> Delete([FromQuery] Guid input)
        {
            var result = await AppService.DeleteAsync(new List<Guid> { input });
            return result;
        }


        [HttpPost]
        public async Task<IActionResult> Import()
        {
            if (Request.Form.Files.Count < 1)
            {
                return new JsonResult(new { error="xxx"});
            }
            var fileStream = Request.Form.Files[0].OpenReadStream();
            var result = await AppService.Import(fileStream);
            return new JsonResult(result);
        }
    }

}
